package com.offcn;

import java.sql.*;
import java.util.Scanner;

public class Login2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //创建连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myschool","root","root");
        //创建sql平台 预编译通道
        PreparedStatement pst = conn.prepareStatement("select * from user where username=? and password=?");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        //给? 占位符赋值

        pst.setString(1,username);
        pst.setString(2,password);
        //执行sql语句
        ResultSet rs = pst.executeQuery();
        if (rs.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        rs.close();
        pst.close();
        conn.close();

    }
}
